﻿using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace VideoCapture
{
    /// <summary>
    /// Device to capture video from camera device
    /// </summary>
    [System.Drawing.ToolboxBitmap(typeof(VideoCaptureDevice), "CAMERA.ICO")] // toolbox bitmap
    [Designer("Sytem.Windows.Forms.Design.ParentControlDesigner,System.Design", typeof(System.ComponentModel.Design.IDesigner))] // make composite
    public class VideoCaptureDevice : System.Windows.Forms.UserControl
    {
        #region Fields

        private System.ComponentModel.IContainer components;
        private int m_FrameRate = 10;
        private int m_Width = 320;
        private int m_Height = 240;
        private ulong m_FrameNumber = 0;

        AForge.Video.DirectShow.VideoCaptureDevice vd;
        string videoSource = string.Empty;

        #endregion

        #region Events

        // ImageCaptured event delegate
        public delegate void CaptureEventHandler(object source, ImageCapturedEventArgs e);
        // fired when a new image is captured
        public event CaptureEventHandler ImageCaptured;

        #endregion

        #region Constructor & Destructor

        public VideoCaptureDevice()
        {
            // This call is required by the Windows.Forms Form Designer.
            InitializeComponent();
        }

        /// <summary>
        /// Override the class's finalize method, so we can stop
        /// the video capture on exit
        /// </summary>
        ~VideoCaptureDevice()
        {
            this.NewStop();
        }

        #endregion

        #region Control Properties

        /// <summary>
        /// The time intervale between frame captures
        /// </summary>
        public int FrameRate
        {
            get
            { return m_FrameRate; }

            set
            { m_FrameRate = value; }
        }

        /// <summary>
        /// The height of the video capture image
        /// </summary>
        public int CaptureHeight
        {
            get
            { return m_Height; }

            set
            { m_Height = value; }
        }

        /// <summary>
        /// The width of the video capture image
        /// </summary>
        public int CaptureWidth
        {
            get
            { return m_Width; }

            set
            { m_Width = value; }
        }

        /// <summary>
        /// The sequence number to start at for the frame number. OPTIONAL
        /// </summary>
        public ulong FrameNumber
        {
            get
            { return m_FrameNumber; }

            set
            { m_FrameNumber = value; }
        }

        #endregion

        #region Component Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            // 
            // WebCamCapture
            // 
            this.Name = "VideoCaptureDevice";
            this.Size = new System.Drawing.Size(342, 252);
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {

                if (components != null)
                    components.Dispose();
            }
            base.Dispose(disposing);
        }

        #endregion

        #region Start & Stop Methods

        public void NewStart()
        {
            if (vd == null || !vd.IsRunning)
            {
                frmCamList form = new frmCamList();
                form.ShowDialog(this);
                videoSource = form.InputDeviceName;
                vd = new AForge.Video.DirectShow.VideoCaptureDevice(videoSource);
                vd.DesiredFrameSize = new System.Drawing.Size(m_Width, m_Height);
                vd.DesiredFrameRate = m_FrameRate;
                vd.NewFrame += new AForge.Video.NewFrameEventHandler(vd_NewFrame);
                vd.Start();
            }
        }

        public void NewStop()
        {
            if (vd != null && vd.IsRunning)
            {
                vd.SignalToStop();
                vd = null;
            }
        }

        void vd_NewFrame(object sender, AForge.Video.NewFrameEventArgs eventArgs)
        {
            ImageCapturedEventArgs x = new ImageCapturedEventArgs();
            if (ImageCaptured != null)
            {
                x.WebCamImage = (System.Drawing.Bitmap)eventArgs.Frame.Clone();
                this.Invoke(ImageCaptured, new object[] { this, x });
            }
        }

        #endregion

        #region ConfigureMethods

        /// <summary>
        /// Shows the VideoFormat dialog
        /// </summary>
        public void ShowVideoFormatDialog()
        {
            //SendMessage(mCapHwnd, WM_CAP_DLG_VIDEOSOURCE, 0, 0);
            vd.DisplayPropertyPage(IntPtr.Zero);
        }


        #endregion

    }
}
